using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Web;
using Decav.Gatsb.Application;

public partial class MasterPage : System.Web.UI.MasterPage, IMasterPageTemplate
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (GatsbApplication.Current.UserPresent)
        {
            meNavLink.HRef = string.Format("~/Person.aspx?id={0}", GatsbApplication.Current.CurrentUser.Id);
            placesNavLink.HRef = string.Format("~/Places.aspx?user={0}", GatsbApplication.Current.CurrentUser.Id);
        }

        if (Request.Browser.Type == "Internet Explorer 6")
            oldBrowserWarning.Visible = true;

        if (Request.Url.Host.ToLower() != "localhost")
        {
            Page.ClientScript.RegisterClientScriptInclude("Google Analytics", "http://www.google-analytics.com/urchin.js");
            Page.ClientScript.RegisterStartupScript(typeof(MasterPage), "Google Analytics Init", "_uacct = \"UA-1751576-1\";urchinTracker();", true);
        }

        ToggleNavVisiblity();
    }

    private void ToggleNavVisiblity()
    {
        bool showNav = HttpContext.Current.User.Identity.IsAuthenticated;
        meNavItem.Visible = showNav;
        friendsNavItem.Visible = showNav;
        helpNavLink.Visible = showNav;
        signoutNavItem.Visible = showNav;

        HtmlControl ctrl = null;
        switch (SelectedPage)
        {
            case MainMenuNavigationButton.None:
                break;

            case MainMenuNavigationButton.Home:
                ctrl = homeNavLink;
                break;

            case MainMenuNavigationButton.MyProfile:
                ctrl = meNavLink;
                break;

            case MainMenuNavigationButton.Friends:
                ctrl = friendsNavLink;
                break;

            case MainMenuNavigationButton.Places:
                ctrl = placesNavLink;
                break;

            case MainMenuNavigationButton.Search:
                ctrl = searchNavLink;
                break;

            case MainMenuNavigationButton.Help:
                ctrl = helpNavLink;
                break;

            default:
                throw new NotImplementedException("The specified MainMenuNavigationButton is not implemented on the master page.");
        }
        homeNavLink.Attributes.Remove("class");
        meNavLink.Attributes.Remove("class");
        friendsNavLink.Attributes.Remove("class");
        placesNavLink.Attributes.Remove("class");
        searchNavLink.Attributes.Remove("class");
        helpNavLink.Attributes.Remove("class");

        if (ctrl != null)
            ctrl.Attributes["class"] = "Selected";
    }

    /// <summary>
    /// Gets or sets the primary page that is currently selected in the navigation.
    /// </summary>
    public MainMenuNavigationButton SelectedPage
    {
        get
        {
            if (ViewState["SelectedPage"] == null)
                return MainMenuNavigationButton.None;

            return (MainMenuNavigationButton)ViewState["SelectedPage"];
        }
        set
        {
            ViewState["SelectedPage"] = value;
            ToggleNavVisiblity();
        }
    }

    protected void SignoutLink_Click(object sender, EventArgs e)
    {
        GatsbWebApplication.Current.SignOut(); 
    }
}
